<?php
/**
 * Ecga数据表格式化接口集
 *
 * PHP Version 5
 *
 * @package    Ecga
 * @subpackage Models
 * @author     v.k <none@none.com>
 * @copyright  2010 Xingchangxinda Inc.
 * @version    SVN:$Id$
 */
namespace Project\Apps\Ecga\Models\Table;

use \Exception;

/**
 * Ecga数据表格式化工厂类
 *
 * @package   Ecga
 * @author    v.k <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   Release: v0.1
 */
class RowFormater
{
    /**
     * 创建格式化对象
     *
     * @param string $name  对象名
     * @param string $table 表名称
     * 
     * @return IRowFormater
     */
    public static function create($name, $table)
    {
        $klass = sprintf('%s\\%sRowFormater', __NAMESPACE__, ucfirst($name));
        return new $klass($table);
    }
}

/**
 * 格式化接口
 *
 * @package   Ecga
 * @author    v.k <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   Release: v0.1
 */
interface IRowFormater
{
    /**
     * 格式化行数据
     * 
     * @param array $row 需要格式化的行数据
     *
     * @return array
     */
    public function format($row);
}

/**
 * Html格式化接口
 *
 * @package   Ecga
 * @author    v.k <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   Release: v0.1
 */
class HtmlRowFormater implements IRowFormater
{
    private $_tableName;
    /**
     * 构造函数
     * 
     * @param string $table 表名称
     */
    public function __construct($table)
    {
        $this->_tableName = $table;
    }
    
    /**
     * 格式化行数据
     * 
     * @param array $row 需要格式化的行数据
     *
     * @return array
     */
    public function format($row)
    {
        if ($this->_tableName == 'productAnalyse') {
            $row = $this->_formatPrudoctAnalyse($row);
        } elseif ($this->_tableName == 'skuAnalyse') {
            $row = $this->_formatSkuAnalyse($row);
        } elseif ($this->_tableName == 'siteAnalyse') {
            $row = $this->_formatSiteAnalyse($row);

        }
        return $row;
    }

    /**
     * 格式化渠道价格变动表
     * 
     * @param array $row 行数据
     * 
     * @return array
     */
    private function _formatSiteAnalyse($row)
    {
        foreach ($row as $key => $value) {
            $format = array();
            if (!in_array($key, array('sku', 'title'))) {
                $format['money'] = true;
                $format['floatRight'] = true;
            }
            $row[$key] = $this->generate($value, $format);
        }

        return $row;
    }

    /**
     * 格式化SKU价格变动表
     * 
     * @param array $row 行数据
     * 
     * @return array
     */
    private function _formatSkuAnalyse($row)
    {
        foreach ($row as $key => $value) {
            $format = array();
            if (!in_array($key, array('sites'))) {
                $format['money'] = true;
                $format['floatRight'] = true;
            }
            $row[$key] = $this->generate($value, $format);
        }

        return $row;
    }
    
    /**
     * 格式化产品价格分析数据
     * 
     * @param array $row 行数据
     * 
     * @return array
     */
    private function _formatPrudoctAnalyse($row)
    {
        $mop = $row['mop'];
        $row['pn'] = sprintf(
            "<a href=\"%s\" class=\"sort-link\">%s</a>",
            'skuAnalyse.php?sku=' . $row['pn'] . 
            '&projectId=' . $row['projectId'] . '&batch=' . $row['batch'] .
            '&date=' . $row['date'],
            $row['pn'] 
        );

        unset($row['date'], $row['projectId'], $row['batch']);

        foreach ($row as $key => $value) {
            $format = array();
            if (!in_array($key, array('sku', 'models', 'time', 'pn', 'level')) 
                && strncmp('url_', $key, 4) !== 0
                && strncmp('title_', $key, 6) !== 0 
            ) {
                $format['money'] = true;
                $format['floatRight'] = true;
            }
            $colorArray = array('sku', 'srp', 'mop',
                'models', 'time', 'pn',
                'level', 'dsp', 'gap', 'tbfp', 'afp');
            if (!in_array($key, $colorArray) 
                && strncmp('url_', $key, 4) !== 0 
                && strncmp('title_', $key, 6) !== 0 
                && $value !== null
            ) {
                if ($value >= $mop) {
                    $format['color'] = 'green';
                }
                if ($value < $mop) {
                    $format['color'] = 'red';
                }
            }
            $row[$key] = $this->generate($value, $format);
            
            $linkArray = array('srp', 'mop', 'sku',
                'models', 'time','gap', 'level',
                'dsp', 'tbfp', 'afp');
            if (!in_array($key, $linkArray)
                && strncmp('url_', $key, 4) !== 0 
                && strncmp('title_', $key, 6) !== 0 
            ) {
                $url = isset($row['url_' . $key]) ? $row['url_' . $key] : null;
                $title = isset($row['title_' . $key]) ? $row['title_' . $key] : null;
                $row[$key] = sprintf(
                    "<a href=\"%s\" target=\"_blank\" title=\"%s\">%s</a>",
                    $url,
                    $title,
                    $row[$key]
                );
            }

        }
        return $row;
    }
    
    /**
     * 生成值的最终格式
     * 
     * @param string $value  值
     * @param array  $format 格式
     * 
     * @return string
     */
    public function generate($value, $format) 
    {
        if (!$format || $format == array()) {
            return $value;
        }
        if (isset($format['money']) && $format['money'] == true) {
            $value = $this->_toMoney($value);
        }
        $style = '';
        if (isset($format['floatRight']) && $format['floatRight'] == true) {
            $style .= 'float: right;';
        }
        if (isset($format['color']) && $format['color'] != '') {
            $style .= 'color: ' . $format['color'];
        }
        return sprintf(
            '<span style="%s">%s</span>',
            $style,
            $value
        );
    }
    
    /**
     * 金钱格式
     * 
     * @param string $val 值
     * 
     * @return string
     */
    private function _toMoney($val) 
    {
        return $val = $val === null ? '-' : number_format($val, 2);
    }
}

/**
 * Excel格式化接口
 *
 * @package   Ecga
 * @author    v.k <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   Release: v0.1
 */
class ExcelRowFormater implements IRowFormater
{
    /**
     * 格式化行数据
     * 
     * @param array $row 需要格式化的行数据
     *
     * @return array
     */
    public function format($row)
    {
        foreach ($row as $key => $val) {
            
            if (substr($key, -13) == 'RelativeRatio') {
                // 环比
                $val = $val == null ? '-' : $val;
            } elseif (substr($key, -5) == 'Ratio') {
                // 比率
                $val = $val === null ? '-' : $val;
            } elseif (substr($key, -3) == 'Dos') {
                // DOS
                $val = $val === null ? '∞' : $val;
            } else {
                $val = $val === null ? '-' : $val;
            }
            
            $row[$key] = $val;
        }
        
        return $row;
    }
}
